package edu.nus.comp.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.nus.comp.dao.BaseDAO;
import edu.nus.comp.dto.CommonDTO;
import edu.nus.comp.dto.SharedPoolDTO;

/**
 * Handles requests for the application home page.
 */
@Controller
public class RealTimeStatusController {
	
	private static final Logger logger = LoggerFactory.getLogger(RealTimeStatusController.class);
	
	@Autowired
	private BaseDAO baseDAO;
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/current", method = RequestMethod.GET)
	public String currentStatus(Locale locale, Model model) {
		logger.info("Welcome To DB Status Center! the client locale is "+ locale.toString());
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate );
		CommonDTO dto = baseDAO.getSharedPoolInfo();
		logger.info("sharedPoolInfo: ratio " + dto.getRatio() );
		model.addAttribute("sharedPoolRatio", dto.getRatio() );
		dto = baseDAO.getRedoLogBufferInfo();
		logger.info("redoLogBufferDTO: ratio " + dto.getRatio() );
		model.addAttribute("redoLogBufferRatio", dto.getRatio() );
		dto = baseDAO.getCacheBufferInfo();
		logger.info("cacheBufferDTO: ratio " + dto.getRatio() );
		model.addAttribute("cacheBufferRatio", dto.getRatio() );
		dto = baseDAO.getSortingAreaInfo();
		logger.info("sortingAreaInfo: ratio " + dto.getRatio() );
		model.addAttribute("sortingAreaRatio", dto.getRatio() );
		
		return "current";
	}
	
}
